package cn.andrewlu.weexshell.weex.core;

import android.graphics.Color;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.andrewlu.weexshell.beans.BottomNav;
import cn.andrewlu.weexshell.beans.NavData;
import cn.andrewlu.weexshell.beans.NavItem;
import cn.andrewlu.weexshell.beans.SinglePageNav;
import cn.andrewlu.weexshell.beans.SlidePageNav;
import cn.andrewlu.weexshell.beans.TopNav;
import cn.andrewlu.weexshell.beans.WXAppModel;
import cn.andrewlu.weexshell.beans.WXPage;

/**
 * Created by andrewlu on 2017/6/5.
 */

public class WXAppParser {
    public final static WXAppModel parse(String appData) {
        WXAppModel instance = new WXAppModel();
        JSONObject app = JSON.parseObject(appData);
        instance.setName(app.getString("name"));
        instance.setVersion(app.getString("version"));
        instance.setVersionCode(app.getInteger("versionCode"));
        instance.setMd5(app.getString("md5"));
        instance.setNavType(app.getString("navType"));
        instance.setNav(parseNav(app));
        JSONArray array = app.getJSONArray("pages");
        if (array != null) {
            Map<String, WXPage> pages = new HashMap<>(array.size());
            int size = array.size();
            for (int i = 0; i < size; i++) {
                JSONObject page = array.getJSONObject(i);
                WXPage p = new WXPage();
                p.setName(page.getString("name"));
                p.setAssets(page.getString("assets"));
                p.setUrl(page.getString("url"));
                p.setTitle(page.getString("title"));
                if (p.getName() == null) continue;
                pages.put(p.getName(), p);
            }
            instance.setPages(pages);
        }

        return instance;
    }

    private static NavData parseNav(JSONObject object) {
        NavData nav = null;
        if (object != null && object.getJSONObject("nav") != null) {
            String navType = object.getString("navType");
            JSONObject navData = object.getJSONObject("nav");

            if (NavType.TYPE_SINGLE_PAGE.equalsIgnoreCase(navType)) {

                nav = new SinglePageNav(navData.getString("pageName"));

            } else if (NavType.TYPE_BOTTOM_NAV.equalsIgnoreCase(navType)) {

                nav = new BottomNav();
                JSONArray navPages = navData.getJSONArray("pages");
                JSONArray navItems = navData.getJSONArray("icons");
                if (navPages != null && navItems != null && navPages.size() == navItems.size()) {
                    int size = navPages.size();
                    List<String> pages = new ArrayList<>(size);
                    List<NavItem> items = new ArrayList<>(size);
                    for (int i = 0; i < size; i++) {
                        pages.add(navPages.getString(i));

                        NavItem item = new NavItem();
                        JSONObject navObj = navItems.getJSONObject(i);
                        item.iconUrl = navObj.getString("iconUrl");
                        item.iconRes = navObj.getString("iconRes");
                        item.text = navObj.getString("text");
                        item.activeColor = Color.parseColor(navObj.getString("activeColor"));
                        item.inActiveColor = Color.parseColor(navObj.getString("inActiveColor"));

                        items.add(item);
                    }
                    ((BottomNav) nav).pages = pages;
                    ((BottomNav) nav).navs = items;
                }
            } else if (NavType.TYPE_TOP_NAV.equalsIgnoreCase(navType)) {

                nav = new TopNav();
                JSONArray navPages = navData.getJSONArray("pages");
                JSONArray navItems = navData.getJSONArray("icons");
                if (navPages != null && navItems != null && navPages.size() == navItems.size()) {
                    int size = navPages.size();
                    List<String> pages = new ArrayList<>(size);
                    List<NavItem> items = new ArrayList<>(size);
                    for (int i = 0; i < size; i++) {
                        pages.add(navPages.getString(i));

                        NavItem item = new NavItem();
                        JSONObject navObj = navItems.getJSONObject(i);
                        item.iconUrl = navObj.getString("iconUrl");
                        item.iconRes = navObj.getString("iconRes");
                        item.text = navObj.getString("text");

                        items.add(item);
                    }
                    ((TopNav) nav).pages = pages;
                    ((TopNav) nav).navs = items;
                }
            } else if (NavType.TYPE_SLIDE_NAV.equalsIgnoreCase(navType)) {
                nav = new SlidePageNav();
                ((SlidePageNav) nav).slidePageName = navData.getString("slidePage");
                JSONObject contentPage = navData.getJSONObject("contentPage");
                ((SlidePageNav) nav).contentPageNav = parseNav(contentPage);
            }
        }
        return nav;
    }
}
